//
//  ZUIVideoIntro.m
//  ZUIVogue
//
//  Created by yaohongchao on 16/12/12.
//  Copyright © 2016年 yaohongchao. All rights reserved.
//

#import "ZUIVideoIntro.h"
//#import <MediaPlayer/MediaPlayer.h>
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>

@interface ZUIVideoIntro ()
@property (nonatomic, strong) AVPlayer *player;
@end

@implementation ZUIVideoIntro

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setui];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)setui{
    self.view.backgroundColor = [UIColor blackColor];
    [UIApplication sharedApplication].statusBarHidden = YES;
    [self.player play];
}

#pragma mark - 懒加载代码
- (AVPlayer *)player{
    if (_player == nil) {
        // 1.获取URL(远程/本地)
         NSURL *url = [[NSBundle mainBundle] URLForResource:@"introduction.mp4" withExtension:nil];
//        NSURL *url = [NSURL URLWithString:@"introduction.mp4"];
        // 2.创建AVPlayerItem
        AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
        // 3.创建AVPlayer
        _player = [AVPlayer playerWithPlayerItem:item];
        // 4.添加AVPlayerLayer
        AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:_player];
        layer.frame = self.view.bounds;
        [self.view.layer addSublayer:layer];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:item];
    }
    return _player;
}

-(void)playbackFinished:(NSNotification *)notification{
    [UIApplication sharedApplication].statusBarHidden = NO;
    if(self.finishBlock) self.finishBlock();
}


-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    self.player = nil;
}

@end


@implementation ZUIVideoIntroWindow : UIWindow

-(void)show{
    self.backgroundColor = [UIColor blackColor];
    ZUIVideoIntro *intro = [[ZUIVideoIntro alloc]init];
    __weak typeof(self) weakSelf = self;
    intro.finishBlock = ^{
        if(weakSelf.finishBlock) weakSelf.finishBlock();
    };
    self.rootViewController = intro;
    [self.window makeKeyAndVisible];
}

@end
